<?php
$error = '缺少所需信息，请再试一次。';
if (checkToken('frontend', $_POST['formToken'], false)  && !empty($_POST['email']) && !empty($_POST['name']) && empty($_POST['additional'])) {
    global $CONFIG,$DBobject,$SMARTY,$SITE;
    //var_dump($_POST);exit;
    $error = '';
    $sent = 0;
    try {
        $banned = array('formToken', 'action', 'additional', 'wantpromo', 'reference_page_objid');
        $content = serialize($_POST);
        $buf .= '<h2>澳睿跑——联系我们表单</h2>';
        foreach ($_POST as $name => $var) {
            if (!in_array($name, $banned)) {
                $buf .= '<br/><b>'.ucwords($name).': </b> <br/> '.$var.'<br/>';
            }
        }
        $body = $buf;
        $subject = '网站联系我们';
        $fromEmail = (string)$CONFIG->company->email_from;
        $to = (string)$CONFIG->company->email_contact;
        if ($_POST['reference_page_objid'] == 119) {
            //franchise enquiry => EMAIL TO ADMIN
            $to = (string) $CONFIG->company->email_franchise_enquiry;
        }

        $COMP = json_encode($CONFIG->company);
        $SMARTY->assign('COMPANY', json_decode($COMP, true));
        $from = (string) $CONFIG->company->name;

        $sent = sendMail($to, $from, $fromEmail, $subject, $body);

        $_SESSION['notice'] = 'Enquiry sent';// Used for Google adword
    } catch (Exception $e) {
        $error = '发送联系邮件时出错。';
    }

    //elc enquiry => EMAIL TO USER
    if ($_POST['reference_page_objid'] == 11) {
        $_SESSION['notice'] = '';
        try {
            $subject = '感谢您向澳睿跑进行咨询';

            $fromEmail = (string) $CONFIG->company->email_from;
            $to = $_POST['email'];
            $SMARTY->assign('name', $_POST['name']);
            $COMP = json_encode($CONFIG->company);
            $SMARTY->assign('COMPANY', json_decode($COMP, true));
            $from = (string) $CONFIG->company->name;
            $body = $SMARTY->fetch('email-elc-enquiry.tpl');
            $sent = sendMail($to, $from, $fromEmail, $subject, $body);
        } catch (Exception $e) {
            $error = '发送联系邮件时出错。';
        }
    }

    //franchise enquiry => EMAIL TO USER
    if ($_POST['reference_page_objid'] == 119) {
        $_SESSION['notice'] = '';
        try {
            $subject = '澳睿跑特许经营商咨询';
            $file = $_SERVER['DOCUMENT_ROOT'].'/uploads/pdfs/Franchise_Snapshot.pdf';
            $attachmentFile = file_exists($file) ? $file : '';
            $fromEmail = (string) $CONFIG->company->email_from;
            $to = $_POST['email'];
            $bcc = '';
            $SMARTY->assign('name', $_POST['name']);
            $COMP = json_encode($CONFIG->company);
            $SMARTY->assign('COMPANY', json_decode($COMP, true));
            $from = (string) $CONFIG->company->name;
            $body = $SMARTY->fetch('email-franchise-enquiry.tpl');
            sendAttachMail($to, $from, $fromEmail, $subject, $body, $bcc, $attachmentFile);
        } catch (Exception $e) {
            $error = '发送联系邮件时出错。';
        }
    }

    //sendGAEvent($GA_ID, 'Enquiry', 'Submitted', $_SERVER['HTTP_REFERER']);

    //============= INSERT RECORD IN DB
    try {
        $content2 = '';
        if (!empty($_POST['schoolname'])) {
            $content2 = $_POST['schoolname'];
        }
        if (!empty($_POST['region'])) {
            $content2 = $_POST['region'];
        }
        if (!empty($_POST['package'])) {
            $content2 = $_POST['package'];
        }

        $sql = 'INSERT INTO tbl_contact (contact_reference_id, contact_reference_name, contact_name, contact_email, contact_phone, contact_postcode, contact_file, contact_enquiry, contact_content1, contact_content2, contact_flag1, contact_flag2, contact_ip,contact_browser, contact_sent)
            VALUES (:contact_reference_id, :contact_reference_name, :contact_name, :contact_email, :contact_phone, :contact_postcode, :contact_file, :contact_enquiry, :contact_content1, :contact_content2, :contact_flag1, :contact_flag2, :contact_ip, :contact_browser, :contact_sent)';
        $contactPostcode = $_POST['address_state'].'|'.$_POST['address_suburb'].'|'.$_POST['address_country'];
        $params = [
            ':contact_reference_id' => $_POST['reference_page_objid'],
            ':contact_reference_name' => $_POST['reference_page'],
            ':contact_name' => $_POST['name'],
            ':contact_email' => $_POST['email'],
            ':contact_phone' => $_POST['phone'],
            ':contact_postcode' => $contactPostcode,
            ':contact_enquiry' => $_POST['enquiry'],
            ':contact_file' => $_POST['file'],
            ':contact_content1' => $_POST['hear_about_us'],
            ':contact_content2' => $content2,
            ':contact_flag1' => $_POST['flag1'],
            ':contact_flag2' => $_POST['flag2'],
            ':contact_ip' => $_SERVER['REMOTE_ADDR'],
            ':contact_browser' => $_SERVER['HTTP_USER_AGENT'],
            ':contact_sent' => $sent,
        ];
        $DBobject->wrappedSql($sql, $params);
    } catch (Exception $e) {
        $error = '保存您的查询时出现意外错误：'.$e;
    }

    if (empty($error)) {
        header('Location: /thank-you');
        die();
    }
}
$_SESSION['error'] = $error;
$_SESSION['post'] = $_POST;
$redirect = $_SERVER['HTTP_REFERER'];
header("Location: {$redirect}#error");
die();
